package com.tcat.user.web;

import com.tcat.common.vo.PageResult;
import com.tcat.user.pojo.Users;
import com.tcat.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "用户管理")
@RestController
@RequestMapping("member")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 分页查询用户
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param search
     * @return
     */
    @ApiOperation(value = "分页查询用户")
    @GetMapping("page")
    public ResponseEntity<PageResult<Users>> queryUsersByPage(
            @RequestParam(value="page",defaultValue = "1") Integer page,
            @RequestParam(value="rows",defaultValue = "5") Integer rows,
            @RequestParam(value="sortBy",required = false) String sortBy,
            @RequestParam(value="desc",defaultValue = "false") Boolean desc,
            @RequestParam(value="search",required = false) String  search
    ){
        return ResponseEntity.ok(userService.queryUsersByPage(page,rows,sortBy,desc,search));
    }



    /**
     * 删除tb_users中的数据，单个删除、多个删除二合一
     * @param uid
     * @return
     */
    @DeleteMapping("uid/{uid}")
    public ResponseEntity<Void> deleteUsers(@PathVariable("uid") String uid){
        String separator="-";
        if(uid.contains(separator)){
            String[] ids=uid.split(separator);
            for(String id:ids){
                this.userService.deleteUsers(Long.parseLong(id));
            }
        }else{
            this.userService.deleteUsers(Long.parseLong(uid));
        }
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 删除tb_urs中的数据
     * @param uid
     * @return
     */
    @DeleteMapping("uid_rid/{uid}")
    public ResponseEntity<Void> deleteByUserIdInUrs(@PathVariable("uid") Long uid){
        userService.deleteByUsersInUrs(uid);
        return ResponseEntity.status(HttpStatus.OK).build();
    }


    /**
     * 新增用户
     * @param user
     * @param rids
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PostMapping
    public ResponseEntity<Void> saveUsers(Users user,@RequestParam("rids") List<Long> rids){
        userService.saveUser(user,rids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 用户修改
     * @param user
     * @param rids
     * @return
     */
    @ApiOperation(value = "修改品牌")
    @PutMapping
    public ResponseEntity<Void> updateUsers(Users user,@RequestParam("rids") List<Long> rids){
        this.userService.updateUser(user,rids);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }


    @GetMapping("/countUsers")
    public int queryUsers(){
        return userService.countUsers();
    }




}
